package com.wen.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {
    private boolean success;
    private int code;  //1失败，0成功
    private String message;
    private T data;

    public static <T> Result<T> ok(T data) {
        return ok(200, "成功", data);
    }

    public static <T> Result<T> ok(int code, String message, T data) {
        return build(true, code, message, data);
    }

    public static <T> Result<T> fail(int code, String message) {
        return fail(code, message, null);
    }

    public static <T> Result<T> fail(int code, String message, T data) {
        return build(false, code, message, data);
    }

    private static <T> Result<T> build(boolean success, int code, String message, T data) {
        Result<T> result = new Result<>();
        result.setSuccess(success);
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }
}